//
//  CRQuestionCollectionViewCell.m
//  NewAge
//
//  Created by 王鑫 on 15/12/3.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CRQuestionCollectionViewCell.h"

@implementation CRQuestionCollectionViewCell

#pragma mark Method init

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatData];
    }
    return self;
}



#pragma mark Method creatData

- (void)creatData {
    self.scrollView = [[UIScrollView alloc] init];
    [self.contentView addSubview:self.scrollView];
    
    self.contentDetailView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentDetailView];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    self.dateLabel = [[UILabel alloc] init];
    self.dateLabel.font = [UIFont fontWithName:@"ArialUnicodeMS" size:15];
    [self.contentDetailView addSubview:self.dateLabel];
    
    self.questionImageView = [[UIImageView alloc] init];
    self.questionImageView.image = [UIImage imageNamed:@"iconfont-icon-wentizixun.png"];
    [self.contentDetailView addSubview:self.questionImageView];
    
    self.questionLabel = [[UILabel alloc] init];
    self.questionLabel.numberOfLines = 0;
    self.questionLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [self.contentDetailView addSubview:self.questionLabel];
    
    self.qContentLabel = [[UILabel alloc] init];
    self.qContentLabel.numberOfLines = 0;
    self.qContentLabel.font = [UIFont systemFontOfSize:16];
    [self.contentDetailView addSubview:self.qContentLabel];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.layer.borderColor = [[UIColor colorWithRed:0.659f green:0.659f blue:0.659f alpha:1.00f] CGColor];
    self.lineView.layer.borderWidth = 1;
    [self.contentDetailView addSubview:self.lineView];
    
    self.askImageView = [[UIImageView alloc] init];
    self.askImageView.image = [UIImage imageNamed:@"iconfont-svgmoban55.png"];
    [self.contentDetailView addSubview:self.askImageView];
    
    self.answerLabel = [[UILabel alloc] init];
    self.answerLabel.numberOfLines = 0;
    self.answerLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    
    [self.contentDetailView addSubview:self.answerLabel];
    
}



#pragma mark - Method layoutSubviews

- (void)layoutSubviews {
    [super layoutSubviews];
    int padding = 10;
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).with.offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.width.mas_equalTo(self.contentView.mas_width);
    }];
    
    [self.contentDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView.mas_width);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView.mas_top).offset(padding);
        make.left.equalTo(self.scrollView.mas_left).offset(padding);
        make.right.equalTo(self.scrollView.mas_right).offset(-padding);
    }];
    
    [self.questionImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dateLabel.mas_bottom).offset(padding);
        make.left.equalTo(self.scrollView.mas_left).offset(padding);
        make.bottom.equalTo(self.qContentLabel.mas_top).offset(-padding);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(45);
    }];
    
    [self.questionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dateLabel.mas_bottom).offset(padding);
        make.left.equalTo(self.questionImageView.mas_right).offset(padding);
        make.bottom.equalTo(self.qContentLabel.mas_top);
        make.right.equalTo(self.scrollView.mas_right);
    }];
    [self.qContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.questionLabel.mas_bottom);
        make.left.equalTo(self.contentDetailView.mas_left).offset(padding);
        make.right.equalTo(self.contentDetailView.mas_right).offset(-padding);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.qContentLabel.mas_bottom).with.offset(padding);
        make.width.equalTo(self.contentDetailView.mas_width);
        make.height.mas_equalTo(1);
    }];
    
    [self.askImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.mas_bottom).offset(padding);
        make.left.equalTo(self.scrollView.mas_left).offset(padding);
        make.bottom.equalTo(self.aContentLabel.mas_top).offset(-padding);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(45);
    }];
    
    [self.answerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.qContentLabel.mas_bottom).offset(padding);
        make.left.equalTo(self.askImageView.mas_right).offset(padding);
        make.bottom.equalTo(self.aContentLabel.mas_top);
        make.right.equalTo(self.scrollView.mas_right).offset(-padding);
    }];
    
    [self.aContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.answerLabel.mas_bottom);
        make.left.equalTo(self.scrollView.mas_left).offset(padding);
        make.right.equalTo(self.scrollView.mas_right).offset(-padding);
    }];
    
    [self.contentDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.aContentLabel.mas_bottom).with.offset(65);
    }];
    
}



#pragma mark - Method setModel

- (void)setModel:(CRQuestionModel *)model {
    if (_model != model) {
        _model = model;
        
        NSString *dayStr = [self.model.strQuestionMarketTime substringFromIndex:self.model.strQuestionMarketTime.length - 2];
        
        NSString *monthStr = [self.model.strQuestionMarketTime substringWithRange:NSMakeRange(self.model.strQuestionMarketTime.length - 5, 2)];
        NSString *yearStr = [self.model.strQuestionMarketTime substringToIndex:4];
        NSInteger month = [monthStr integerValue];
        switch (month) {
            case 1:
                self.dateLabel.text = [NSString stringWithFormat:@"January %@,%@", dayStr, yearStr];
                break;
            case 2:
                self.dateLabel.text = [NSString stringWithFormat:@"February %@,%@", dayStr, yearStr];
                break;
            case 3:
                self.dateLabel.text = [NSString stringWithFormat:@"March %@,%@", dayStr, yearStr];
                break;
            case 4:
                self.dateLabel.text = [NSString stringWithFormat:@"April %@,%@", dayStr, yearStr];
                break;
            case 5:
                self.dateLabel.text = [NSString stringWithFormat:@"May %@,%@", dayStr, yearStr];
                break;
            case 6:
                self.dateLabel.text = [NSString stringWithFormat:@"June %@,%@", dayStr, yearStr];
                break;
            case 7:
                self.dateLabel.text = [NSString stringWithFormat:@"July %@,%@", dayStr, yearStr];
                break;
            case 8:
                self.dateLabel.text = [NSString stringWithFormat:@"August %@,%@", dayStr, yearStr];
                break;
            case 9:
                self.dateLabel.text = [NSString stringWithFormat:@"September %@,%@", dayStr, yearStr];
                break;
            case 10:
                self.dateLabel.text = [NSString stringWithFormat:@"October %@,%@", dayStr, yearStr];
                break;
            case 11:
                self.dateLabel.text = [NSString stringWithFormat:@"November %@,%@", dayStr, yearStr];
                break;
            case 12:
                self.dateLabel.text = [NSString stringWithFormat:@"December %@,%@", dayStr, yearStr];
                break;
        
            default:
                break;
        }
        NSLog(@"%@", self.model.strQuestionTitle);
        NSMutableAttributedString *qAttributedString = [[NSMutableAttributedString alloc]initWithString:self.model.strQuestionTitle];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        [paragraphStyle setLineSpacing:5];
        [qAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.model.strQuestionTitle.length)];
        self.questionLabel.attributedText = qAttributedString;
        
        NSMutableAttributedString *qcAttributedString = [[NSMutableAttributedString alloc]initWithString:self.model.strQuestionContent];
        NSMutableParagraphStyle *qcParagraphStyle = [[NSMutableParagraphStyle alloc]init];
        [qcParagraphStyle setLineSpacing:10];
        [qcAttributedString addAttribute:NSParagraphStyleAttributeName value:qcParagraphStyle range:NSMakeRange(0, self.model.strQuestionContent.length)];
        self.qContentLabel.attributedText = qcAttributedString;
        [self.qContentLabel sizeToFit];
        
        NSMutableAttributedString *aAttributedString = [[NSMutableAttributedString alloc]initWithString:self.model.strAnswerTitle];
        NSMutableParagraphStyle *aParagraphStyle = [[NSMutableParagraphStyle alloc]init];
        [paragraphStyle setLineSpacing:5];
        [aAttributedString addAttribute:NSParagraphStyleAttributeName value:aParagraphStyle range:NSMakeRange(0, self.model.strAnswerTitle.length)];
        self.answerLabel.attributedText = aAttributedString;
        
        self.aContentLabel = [[UILabel alloc] init];
        self.aContentLabel.numberOfLines = 0;
        self.aContentLabel.font = [UIFont systemFontOfSize:16];
        self.aContent = self.model.strAnswerContent;
        self.aContent = [self.aContent stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
        self.aContent = [self.aContent stringByReplacingOccurrencesOfString:@"<i>" withString:@""];
        self.aContent = [self.aContent stringByReplacingOccurrencesOfString:@"</i>" withString:@""];
        [self.contentDetailView addSubview:self.aContentLabel];
        NSLog(@"%@", self.aContent);
        NSMutableAttributedString *acAttributedString = [[NSMutableAttributedString alloc]initWithString:self.aContent];
        NSMutableParagraphStyle *acParagraphStyle = [[NSMutableParagraphStyle alloc]init];
        [acParagraphStyle setLineSpacing:10];
        [acAttributedString addAttribute:NSParagraphStyleAttributeName value:acParagraphStyle range:NSMakeRange(0, self.aContent.length)];
        self.aContentLabel.attributedText = acAttributedString;
        [self.aContentLabel sizeToFit];
        
    }
}

@end


